<?php
class helper extends CApplicationComponent {

    function dateFormating($date) {
        list($day, $month, $year) = explode('/', $date);
		$date_f = $year."-".$month."-".$day;
        return $date_f;
    }
	
	function dateFormatingSlash($date) {
        list($day, $month, $year) = explode('-', $date);
		$date_f = $year."-".$month."-".$day;
        return $date_f;
    }
	
	function dateTimeFormating($date) {
		$dt = explode(' ', $date);
        list($day, $month, $year) = explode('-', $dt[0]);
		$date_f = $year."-".$month."-".$day;
        return $date_f;
    }
	
	function dateTimeFullFormating($date) {
		$dt = explode(' ', $date);
        list($day, $month, $year) = explode('-', $dt[0]);
		$date_f = $year."-".$month."-".$day;
        return $date_f." ".$dt[1];
    }
	
	function dateFormatingDef($date) {
        list($year, $month, $day) = explode('-', $date);
		// $date_f = $year."-".$month."-".$day;
		$date_f = $day."/".$month."/".$year;
        return $date_f;
    }
	
	function moneyFormat($number){
		$num = number_format($number, 0, ',', '.');
		return $num;
	}
	
	function decomMoney($number){
		return intval(str_replace(".","",$number));
	}
	
	function hotele_code($length = 10, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890') {
        $chars_length = (strlen($chars) - 1);
        $string = $chars{rand(0, $chars_length)};
        for ($i = 1; $i < $length; $i = strlen($string)) {
            $r = $chars{rand(0, $chars_length)};
            if ($r != $string{$i - 1})
                $string .= $r;
        }
        return $string;
    }

    function cek_code($code, $tabels = 'GuestTrx', $fields = 'checkin_code') {
        //$tabel_code = array('checkin', 'pos_transactions');               
        $codeExist = $tabels::model()->find($fields . ' = "' . $code . '"')->id;
        if ($codeExist != '') {
            $result = 1;
        } else {
            $result = 0;
        }
        return $result;
    }
	
	function cek_codeAr($code, $tabels = 'GuestPayment', $fields = 'ar_generate_number') {
        //$tabel_code = array('checkin', 'pos_transactions');               
        $codeExist = $tabels::model()->find($fields . ' = "' . $code . '"')->id;
        if ($codeExist != '') {
            $result = 1;
        } else {
            $result = 0;
        }
        return $result;
    }
	
	function formating_numb($value, $threshold = 2) {
		return sprintf('%0' . $threshold . 's', $value);
	}
	
	function numb_code($length, $tabels, $fields) {            
        // $sql = "select max($fields)as max_id from $tabels";
		$sql = "SELECT max(cast(SUBSTRING($fields,3) AS SIGNED )) as max_id FROM $tabels";
		$rs = Yii::app()->db->createCommand($sql)->queryAll();
		return $this->formating_numb(($rs[0]['max_id']+1), $length);
    }
	
	public function getDatesBetween2Dates($startTime, $endTime, $formatYear) {

        $day = 86400;
        if ($formatYear == 2) {
            $format = 'Y-m-d';
        } else {
            $format = 'd/m';
        }
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        $numDays = round(($endTime - $startTime) / $day) + 1;
        $days = array();

        for ($i = 0; $i < $numDays; $i++) {
            $days[] = date($format, ($startTime + ($i * $day)));
        }

        if ($startTime == $endTime) {
            $days = array();
            array_push($days, 1);
        }

        return $days;
    }

}

?>